package com.api.qfengx;

import com.alibaba.fastjson.JSON;
import com.weaver.qfengx.*;
import com.weaver.qfengx.api.ec.EcApiService;
import com.weaver.qfengx.entity.Result;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.workflow.workflow.WorkflowComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.IOException;

/**
 * @Description API 分发器
 * @Author Qfengx <qffg1998@126.com>
 * @Date 2021-07-12 23:51:15
 */

@Path("/qfengx/dispatch")
public class DispatchApi {

    public static void main(String[] args) {
        String s = "$showVal$";
        System.out.println(s.replaceAll("\\$showVal\\$", StringUtils.val(null, "")));
    }

    private LogUtils log = new LogUtils(DispatchApi.class);

    @GET
    @Path("/date")
    @Produces(MediaType.APPLICATION_JSON)
    public String date(@Context HttpServletRequest request,
                            @Context HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        return JSON.toJSONString(Result.ok(DateUtils.datetime()));
    }

    @GET
    @Path("/userinfo")
    @Produces(MediaType.APPLICATION_JSON)
    public String userinfo(@Context HttpServletRequest request,
                       @Context HttpServletResponse response) throws IOException {
        return JSON.toJSONString(Result.ok(HrmUserVarify.getUser(request, response)));
    }

    @GET
    @Path("/testApi")
    @Produces(MediaType.APPLICATION_JSON)
    public String testApi(@Context HttpServletRequest request,
                       @Context HttpServletResponse response) throws IOException {
        String appid = request.getParameter("appid");
        String userid = request.getParameter("userid");
        if (StringUtils.isEmpty(userid)) userid = "1";
        EcApiService apiService = new EcApiService("http://localhost:8082", appid);
        try {
            // 获取用户的代办信息
            return apiService.requestApiPost("/api/workflow/paService/getToDoWorkflowRequestList", userid).body().string();
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(Result.exception().setMsg(e.getMessage()));
        }
    }


    @GET
    @Path("/field")
    @Produces(MediaType.APPLICATION_JSON)
    public String field(@Context HttpServletRequest request,
                       @Context HttpServletResponse response) throws IOException {
        String path = request.getParameter("service");
        String wid = request.getParameter("wid");
        String rid = request.getParameter("rid");
        String value = request.getParameter("val");
        String fieldname = request.getParameter("fieldname");
        WorkflowComInfo workflowComInfo = new WorkflowComInfo();
        String formid = Integer.toString(Math.abs(Util.getIntValue(workflowComInfo.getFormId(wid))));
        if (StringUtils.isEmpty(path)) {
            return JSON.toJSONString(FieldUtils.getFieldValueByFieldnameAndDetailTablename(fieldname, rid, formid,
                    weaver.general.Util.getIntValue(wid), value, WorkflowUtils.mainTable(wid)));
        }
        response.sendRedirect(path);
        return JSON.toJSONString(Result.ok(DateUtils.datetime()));
    }
}
